package com.saikei.demoapp.titlebar.utils

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.util.DisplayMetrics
import android.view.View
import android.view.inputmethod.InputMethodManager

object ScreenUtils {
    fun Context.dp2Px(dp: Float): Float {
        return dp * density(this)
    }

    fun Context.px2Dp(px: Float): Float {
        return px / density(this)
    }

    fun density(context: Context): Float {
        return context.resources.displayMetrics.density
    }


    fun dp2PxInt(context: Context, dp: Float): Int {
        return (context.dp2Px(dp) + 0.5f).toInt()
    }

    fun px2DpCeilInt(context: Context, px: Float): Float {
        return (context.px2Dp(px) + 0.5f).toInt().toFloat()
    }


    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    fun getDisplayMetrics(context: Context): DisplayMetrics {
        val activity = if (context !is Activity && context is ContextWrapper) {
            context.baseContext as Activity
        } else {
            context as Activity
        }
        val metrics = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(metrics)
        return metrics
    }

    /**
     * 获取屏幕大小
     *
     * @param context
     * @return
     */
    fun getScreenPixelSize(context: Context): IntArray {
        val metrics = getDisplayMetrics(context)
        return intArrayOf(metrics.widthPixels, metrics.heightPixels)
    }

    fun Context.hideSoftInputKeyBoard(focusView: View?) {
        if (focusView != null) {
            val binder = focusView.windowToken
            if (binder != null) {
                val imd = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imd.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_IMPLICIT_ONLY)
            }
        }
    }

    fun Context.showSoftInputKeyBoard(focusView: View?) {
        focusView?.let {
            val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(it, InputMethodManager.SHOW_FORCED)
        }
    }

    fun getScreenWidth(context: Context): Int {
        return context.resources.displayMetrics.widthPixels
    }

    fun getScreenHeight(context: Context): Int {
        return context.resources.displayMetrics.heightPixels
    }

    fun getStatusBarHeight(context: Context): Int {
        var statusBarHeight = 0
        try {
            val c = Class.forName("com.android.internal.R\$dimen")
            val obj = c.newInstance()
            val field = c.getField("status_bar_height")
            val x = field[obj].toString().toInt()
            statusBarHeight = context.resources.getDimensionPixelSize(x)
        } catch (e1: Exception) {
            e1.printStackTrace()
        }
        return statusBarHeight
    }

    fun getAppInScreenHeight(context: Context): Int {
        return getScreenHeight(context) - getStatusBarHeight(context)
    }
}